from manim import *
import math
class work22(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        self.camera.frame.save_state()
        self.play(self.camera.frame.animate.scale(0.5).move_to([0,0,0]))
        text1 = Tex(r"\text{两种理解角度}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text1))
        text2 = Tex(r"\text{1.定义推导}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
      
        text3 = Tex(r"\text{2.几何意义}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        
        text4 = Tex(r"\text{1.定义推导}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,25,0])
        text40 = Tex(r"\text{1.几何意义}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,25,0])
        self.play(
            ReplacementTransform(text1,text2)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text2,text3)
        )
        self.wait(0.5)
        self.play(
            self.camera.frame.animate.scale(2).move_to([0,0,0]),
            ReplacementTransform(text3,text4)
        )
        self.wait(0.5)

        text5 = Tex(r"$F\left( x\right) =\int _{a}^{x}f\left( t\right) dt$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text5))
        self.play(text5.animate().scale(1).move_to(UP*20)) 
        text6 = Tex(r"$F\left( x+T\right) =\int _{a}^{x+T}f\left( t\right) dt$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text6))
        self.play(text6.animate().scale(1).move_to(LEFT*20))
        text7 = Tex(r"$=\int _{a}^{x}f\left( t\right) dt+\int _{x}^{x+T}f\left( t\right) dt$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,RIGHT,buff=1)
        self.play(Write(text7))
        self.play(text6.animate().scale(1).shift(UP*10),text7.animate().scale(1).shift(UP*10))
        #画横线
        #下划线
        #下划线
        dot1=Dot([-1,7,0])
        dot2=Dot([9,7,0])
        line=Line(dot1,dot2).set_color(PURPLE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        text80 = Tex(r"$F\left( x\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(line,DOWN,buff=2)
        self.play(Write(text80))
        #下划线
        dot3=Dot([12,7,0])
        dot4=Dot([26,7,0])
        line1=Line(dot1,dot2).set_color(PURPLE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line1))
        #箭头
        arrow = Arrow(start=[19,6,0],end=[19,-4,0],color=RED
                       , stroke_width=20, tip_length=1)

        
        text8 = Tex(r"$\int _{0}^{T}f\left( t\right) dt$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([19,-9,0])
        text9 = Tex(r"\text{性质1.}$\int _{a}^{a+T}f\left( x\right) dx=\int _{0}^{T}f\left( x\right) dx$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-18,-2,0])
        text10 = Tex(r"\text{性质2.}$\int ^{b+T}_{a+T}f\left( x\right) dx=\int ^{b}_{a}f\left( x\right) dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-18,-8,0])
        self.play(GrowArrow(arrow),Write(text9),Write(text10))
        self.play(Write(text8))
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=11,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([19,-9,0])
        self.play(Create(rectangle))
        text11 = Tex(r"$=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle,RIGHT,buff=1)
        self.play(Write(text11))
        text12 = Tex(r"$F\left( x+T\right) =F\left( x\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([0,-20,0])
        self.play(Write(text12))
        self.wait(0.5)
        #销毁
        self.play(Uncreate(text6),Uncreate(text7),Uncreate(text8),Uncreate(text9)
                  ,Uncreate(text10),Uncreate(text11),Uncreate(text12),
                  Uncreate(line),Uncreate(line1),FadeOut(rectangle),Uncreate(text80),Uncreate(arrow)
                  ,ReplacementTransform(text4,text40))
        #画fx＝sinx

         #画坐标轴

        axes = Axes(
            x_range=[-PI, 4*PI, PI],  # 设置 x 轴范围，从 -3π 到 3π，步长为 π/2
            y_range=[-1.5, 1.5, 0.5],     # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE,"stroke_width": 30},  # 设置坐标轴的颜色
            x_length=40,  # 控制 x 轴长度
            y_length=20 ,   # 控制 y 轴长度
        )
        axes.move_to([-25,-10,0])
        # 定义要绘制的函数
        def my_function(x):
            return math.sin(x)
        graph1 = axes.plot(my_function, x_range=[-PI, 4*PI], color=BLUE, stroke_width=20)
        # 定义直线函数
        def constant_line(x):
            return 0  # 这里的常数是示例值，你可以根据实际需要调整

        # 创建直线图像
        line_graph = axes.plot(constant_line, x_range=[-PI, 4*PI], color=PURE_RED, stroke_width=20)
        # 添加标题
        title = Text("f(x)=sinx",color=GREEN, font_size=240).move_to([-35,10,0])
        title1 = Text("f(x)=sinx+1",color=GREEN, font_size=240).move_to([-35,10,0])
        self.add(title)

        # 显示图像
        self.play(Create(axes),  Write(title),Create(graph1))
        self.wait()
        #
        text130 = Tex(r"$\int _{0}^{2\pi }f\left( x\right) dx=0$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,0,0])
        text131 = Tex(r"$\int _{0}^{2\pi }f\left( x\right) dx\neq 0$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,0,0])
        self.play(Write(text130))

        dot4=Dot([-37,5,0])
        dot5=Dot([-37,-20,0])
        line2=Line(dot4,dot5).set_color(PURPLE)
        line2.set_stroke(width=30)  # 设置宽度为 30
        text13 = Tex(r"$=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([25,20,0])
        self.play(Create(line2),Write(text13))

        area2 = axes.get_area(graph1, [0, 0], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area3 = axes.get_area(graph1, [0, PI/2], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area4 = axes.get_area(graph1, [PI/2, PI], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area5 = axes.get_area(graph1, [PI, 3*PI/2], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area6 = axes.get_area(graph1, [3*PI/2, 2*PI], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area7 = axes.get_area(graph1, [2*PI, 5*PI/2], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area8 = axes.get_area(graph1, [5*PI/2, 3*PI], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area9 = axes.get_area(graph1, [3*PI, 7*PI/2], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area10 = axes.get_area(graph1, [7*PI/2, 4*PI], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
     
        # 也要添加到场景中
        self.play(Create(area2))

        
        text14 = Tex(r"$=1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([25,20,0])
        text15 = Tex(r"$=2$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([25,20,0])
        text16 = Tex(r"$=1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([25,20,0])
        text17 = Tex(r"$=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([25,20,0])
        text18 = Tex(r"$=1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([25,20,0])
        text19 = Tex(r"$=2$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([25,20,0])
        text20 = Tex(r"$=1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([25,20,0])
        text21 = Tex(r"$=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([25,20,0])
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                  ReplacementTransform(text13,text14),
                  Create(area3))
                  
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text14,text15),
                  Create(area4))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text15,text16),
                  Create(area5))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text16,text17),
                  Create(area6))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                  ReplacementTransform(text17,text18),
                  Create(area7))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text18,text19),
                  Create(area8))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text19,text20),
                  Create(area9))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text20,text21),
                  Create(area10))
        self.play(
                   ReplacementTransform(title,title1),
                   ReplacementTransform(text130,text131))
        #重新定义函数
        # 定义要绘制的函数
        def my_function(x):
            return math.sin(x)+1
        graph2 = axes.plot(my_function, x_range=[-PI, 4*PI], color=BLUE, stroke_width=20)

        self.wait()
        self.play(graph1.animate().shift(UP*7),line2.animate().scale(1).shift(LEFT*32)
                   ,
                  Create(area2),
                  FadeOut(area3),FadeOut(area4),FadeOut(area5),FadeOut(area6),FadeOut(area7),FadeOut(area8),FadeOut(area9)
                  ,FadeOut(area10))
        area2 = axes.get_area(graph2, [0, 0], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area3 = axes.get_area(graph2, [0, PI/2], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area4 = axes.get_area(graph2, [PI/2, PI], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area5 = axes.get_area(graph2, [PI, 3*PI/2], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area6 = axes.get_area(graph2, [3*PI/2, 2*PI], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area7 = axes.get_area(graph2, [2*PI, 5*PI/2], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area8 = axes.get_area(graph2, [5*PI/2, 3*PI], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area9 = axes.get_area(graph2, [3*PI, 7*PI/2], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
        area10 = axes.get_area(graph2, [7*PI/2, 4*PI], bounded_graph=line_graph, color=PURPLE, opacity=0.5)
     
        
        text24 = Tex(r"$=1+\dfrac{\pi }{2}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,20,0])
        text25 = Tex(r"$=2+\pi $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,20,0])
        text26 = Tex(r"$=1+\dfrac{3\pi }{2}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,20,0])
        text27 = Tex(r"$=0+2\pi $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,20,0])
        text28 = Tex(r"$=1+\dfrac{5}{2}\pi $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,20,0])
        text29 = Tex(r"$=2+3\pi $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,20,0])
        text30 = Tex(r"$=1+\dfrac{7}{2}\pi $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,20,0])
        text31 = Tex(r"$=0+4\pi $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([20,20,0])
        
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                  ReplacementTransform(text21,text24),
                  Create(area3))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text24,text25),
                  Create(area4))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text25,text26),
                  Create(area5))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text26,text27),
                  Create(area6))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                  ReplacementTransform(text27,text28),
                  Create(area7))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text28,text29),
                  Create(area8))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text29,text30),
                  Create(area9))
        self.play(line2.animate().scale(1).shift(RIGHT*4),
                   ReplacementTransform(text30,text31),
                  Create(area10))


        self.wait()
